package com.yekaile.carsystem.controller;


import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yekaile.carsystem.entity.SystemAuthority;
import com.yekaile.carsystem.service.AuthorityService;
import com.yekaile.carsystem.util.Result;
import lombok.AllArgsConstructor;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yekaile
 */
@RestController
@RequestMapping("authority")
@AllArgsConstructor(onConstructor_=@Autowired)
public class AuthorityController {

    AuthorityService authorityService;


    @RequiresPermissions("authority:add")
    @PostMapping("addAuthority")
    public Result<String> addAuthority(@RequestBody SystemAuthority systemAuthority) {
        systemAuthority.setGmtCreated(DateUtil.now());
        authorityService.addAuthority(systemAuthority);
        return new Result<>(HttpStatus.SC_CREATED, "success", "success");
    }

    @RequiresPermissions("authority:select")
    @GetMapping("getAllAuthority")
    public Result<PageInfo<SystemAuthority>> listAllAuthority(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                                                             @RequestParam(defaultValue = "8", required = false) Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SystemAuthority> authorities = authorityService.getAllAuthorities();
        PageInfo<SystemAuthority> pageInfo = new PageInfo<>(authorities);
        return new Result<>(HttpStatus.SC_OK, "success", pageInfo);
    }

    @RequiresPermissions("authority:delete")
    @DeleteMapping("deleteAuthority/{id}")
    public Result<String> deleteAuthority(@PathVariable("id") Integer authorityId) {
        authorityService.deleteAuthority(authorityId);
        return new Result<>(HttpStatus.SC_NO_CONTENT, "success", "success");
    }


}
